package com.livewwallpaper.pongur;

/**
 * Android Live Wallpaper Archetype
 * @author antoine vianey
 * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 */

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class PongUr extends WallpaperService {  
	
	@Override
    public Engine onCreateEngine() {
        return new PongurEngine();
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
 
    public class PongurEngine extends Engine implements OnSharedPreferenceChangeListener{
 
        private static final String SHARED_PREFS_NAME = "pongursettings";
		private final PongUrPaint painting;
        private SharedPreferences mPrefs;
 
        PongurEngine() {
        	mPrefs = PongUr.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
            mPrefs.registerOnSharedPreferenceChangeListener(this);
            final SurfaceHolder holder = getSurfaceHolder();
            painting = new PongUrPaint(holder, 
                getApplicationContext(), mPrefs);
        }
 
        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            mPrefs = PongUr.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
            mPrefs.registerOnSharedPreferenceChangeListener(this);
            setTouchEventsEnabled(true);
        }
 
        @Override
        public void onDestroy() {
            super.onDestroy();
            mPrefs.unregisterOnSharedPreferenceChangeListener(this);
            painting.stopPainting();
        }
 
        @Override
        public void onVisibilityChanged(boolean visible) {
            if (visible) {
            	mPrefs = PongUr.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
                mPrefs.registerOnSharedPreferenceChangeListener(this);
                painting.resumePainting();
            } else {
            	mPrefs.unregisterOnSharedPreferenceChangeListener(this);
                painting.pausePainting();
            }
        }
 
        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, 
                int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            painting.setSurfaceSize(width, height);
        }
 
        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            // start painting
            painting.start();
        }
 
        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            boolean retry = true;
            painting.stopPainting();
            while (retry) {
                try {
                    painting.join();
                    retry = false;
                } catch (final InterruptedException e) {}
            }
        }
 
        @Override
        public void onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            painting.doTouchEvent(event);
        }

		@Override
		public void onSharedPreferenceChanged(
			SharedPreferences sharedPreferences, String key) {
			final String shape = sharedPreferences.getString("pongur_shape", "cube");
			final String speed = mPrefs.getString("pongur_speed", "2");
			final String color = sharedPreferences.getString("pongur_color", "black");
			final boolean gameplay = sharedPreferences.getBoolean("gameplay", false);
			final boolean showclock = sharedPreferences.getBoolean("showclock", true);
			final boolean showshadow = sharedPreferences.getBoolean("showshadow", false);
			final boolean showdash = sharedPreferences.getBoolean("showdash", true);
			final String scorepos = sharedPreferences.getString("pongur_scorepos", "two");
			if(shape.equals("cube"))
				painting.setShape(0);
			else
				painting.setShape(1);
			painting.setSpeed(speed);
			painting.setShowShadow(showshadow);
			painting.setColor(color);
			painting.setScorePos(scorepos);
			painting.setGamePlay(gameplay);
			painting.setShowClock(showclock);
			painting.setShowDash(showdash);
		}

 
    }

}